<?php

namespace App\Task;

use App\Service\EventOrderService;
use App\Service\OfficeService;
use App\Service\MchAccountService;
use App\Service\StatMonthlyTradeService;
use App\Service\TradeOrderDetailService;
use App\Utils\CustomUtils;
use Hyperf\Contract\TranslatorInterface;
use Hyperf\Di\Annotation\Inject;
use Hyperf\Task\Annotation\Task;
use Hyperf\Utils\Arr;

class ExportTask {

    /**
     * @Inject()
     * @var OfficeService
     */
    private $OfficeService;

    /**
     * @Inject()
     * @var TradeOrderDetailService
     */
    private $TradeOrderDetailService;

    /**
     * @Inject()
     * @var MchAccountService
     */
    private $MchAccountService;

    /**
     * @Inject()
     * @var EventOrderService
     */
    private $EventOrderService;

    /**
     * @Inject()
     * @var StatMonthlyTradeService
     */
    private $StatMonthlyTradeService;

    /**
     * @Inject
     * @var TranslatorInterface
     */
    private $translator;

    /**
     * @Task
     */
    public function handle (array $param): bool {
        $locale = Arr::get($param, 'locale');

        if ($locale) {
            $this->translator->setLocale($locale);
        }

        $logId       = $param['logId'];
        $type        = $param['type'];
        $searchParam = $param['param'];
        $xlsCell     = $param['xlsCell'];
        $fileName    = $param['fileName'];

        CustomUtils::xzxDebug("接收导出任务，导出类型{$type}，导出参数：".json_encode($searchParam, JSON_UNESCAPED_UNICODE)."，logId:{$logId} fileName:{$fileName}");

        $startTime = time();
        // 根据类型来处理，取出数据
        $xlsData = [];

        // type 1.订单 2.商户账户 3.操作订单 4.统计报表-商户/渠道 5.商户/渠道数据统计
        switch ($type) {
            case 1:
                $result  = $this->TradeOrderDetailService->list($searchParam);
                $xlsData = $result['data']['list'];
                break;
            case 2:
                $result  = $this->MchAccountService->list($searchParam);
                $xlsData = $result['data']['list'];
                break;
            case 3:
                $result  = $this->EventOrderService->list($searchParam);
                $xlsData = $result['data']['list'];
                break;
            case 4:
                if ($searchParam['type'] == 'mch') {
                    $result  = $this->StatMonthlyTradeService->financeMchList($searchParam);
                    $xlsData = $result['data']['list'];
                }
                else {
                    $result  = $this->StatMonthlyTradeService->financeChannelList($searchParam);
                    $xlsData = $result['data']['list'];
                }
                break;
            case 5:
                if ($searchParam['type'] == 'mch') {
                    $result  = $this->StatMonthlyTradeService->statMchList($searchParam);
                    $xlsData = $result['data']['list'];
                }
                else {
                    $result  = $this->StatMonthlyTradeService->statChannelList($searchParam);
                    $xlsData = $result['data']['list'];
                }
                break;
        }

        CustomUtils::xzxDebug("已获取数据，条数：".count($xlsData)."，正在生成xlsx");
        $this->OfficeService->exportExcel($fileName, $xlsCell, $xlsData, $logId);
        $endTime = time();
        CustomUtils::xzxDebug("已生成xlsx，耗时：".($endTime - $startTime)."s");

        return true;
    }
}
